<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自动重组进度</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            margin: 0;
            padding: 10px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: #333;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            overflow: hidden;
        }
        
        .progress-container {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 10px;
            padding: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            width: 420px;
            max-height: 280px;
            text-align: center;
            overflow: hidden;
        }
        
        .title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 12px;
            color: #2c3e50;
        }
        
        .status-text {
            font-size: 13px;
            margin-bottom: 12px;
            color: #555;
            min-height: 16px;
        }
        
        .progress-wrapper {
            margin: 12px 0;
        }
        
        .progress-bar {
            width: 100%;
            height: 12px;
            background: #e0e0e0;
            border-radius: 6px;
            overflow: hidden;
            position: relative;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(90deg, #4CAF50, #45a049);
            border-radius: 6px;
            width: 0%;
            transition: width 0.3s ease;
            position: relative;
        }
        
        .progress-fill::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
            animation: shimmer 2s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: translateX(-100%); }
            100% { transform: translateX(100%); }
        }
        
        .progress-text {
            margin-top: 8px;
            font-size: 13px;
            color: #666;
        }
        
        .details {
            margin-top: 12px;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 6px;
            border-left: 3px solid #4CAF50;
        }
        
        .detail-item {
            display: flex;
            justify-content: space-between;
            margin: 3px 0;
            font-size: 12px;
        }
        
        .detail-label {
            font-weight: 500;
            color: #555;
        }
        
        .detail-value {
            color: #333;
            font-weight: 600;
        }
        
        .spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #4CAF50;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-right: 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .completed {
            color: #4CAF50;
            font-weight: 600;
        }
        
        .error {
            color: #f44336;
            font-weight: 600;
        }
        
        .abort-btn {
            margin-top: 12px;
            margin-right: 8px;
            padding: 8px 16px;
            background: #f44336;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 13px;
            display: inline-block;
        }
        
        .abort-btn:hover {
            background: #da190b;
        }
        
        .abort-btn:disabled {
            background: #cccccc;
            cursor: not-allowed;
        }
        
        .close-btn {
            margin-top: 12px;
            padding: 8px 16px;
            background: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 13px;
            display: none;
        }
        
        .close-btn:hover {
            background: #45a049;
        }
    </style>
</head>
<body>
    <div class="progress-container">
        <div class="title">自动重组操作进度</div>
        
        <div class="status-text" id="statusText">
            <span class="spinner"></span>正在初始化...
        </div>
        
        <div class="progress-wrapper">
            <div class="progress-bar">
                <div class="progress-fill" id="progressFill"></div>
            </div>
            <div class="progress-text" id="progressText">0%</div>
        </div>
        
        <div class="details">
            <div class="detail-item">
                <span class="detail-label">当前处理:</span>
                <span class="detail-value" id="currentItem">-</span>
            </div>
            <div class="detail-item">
                <span class="detail-label">已处理:</span>
                <span class="detail-value" id="processedCount">0</span>
            </div>
            <div class="detail-item">
                <span class="detail-label">总数:</span>
                <span class="detail-value" id="totalCount">0</span>
            </div>
            <div class="detail-item">
                <span class="detail-label">耗时:</span>
                <span class="detail-value" id="elapsedTime">0秒</span>
            </div>
        </div>
        
        <button class="abort-btn" id="abortBtn" onclick="abortOperation()" style="display: inline-block;">中止操作</button>
        <button class="close-btn" id="closeBtn" onclick="closeDialog()" style="display: none;">关闭</button>
    </div>
    
    <script>
        let startTime = Date.now();
        let timerInterval;
        
        // 启动计时器
        function startTimer() {
            timerInterval = setInterval(updateElapsedTime, 1000);
        }
        
        // 更新耗时显示
        function updateElapsedTime() {
            const elapsed = Math.floor((Date.now() - startTime) / 1000);
            document.getElementById('elapsedTime').textContent = elapsed + '秒';
        }
        
        // 更新进度
        function updateProgress(data) {
            const { 
                progress = 0, 
                status = '', 
                currentItem = '', 
                processedCount = 0, 
                totalCount = 0,
                isCompleted = false,
                isError = false,
                isAborted = false
            } = data;
            
            // 更新进度条
            const progressFill = document.getElementById('progressFill');
            const progressText = document.getElementById('progressText');
            progressFill.style.width = progress + '%';
            progressText.textContent = Math.round(progress) + '%';
            
            // 更新状态文本
            const statusText = document.getElementById('statusText');
            const abortBtn = document.getElementById('abortBtn');
            const closeBtn = document.getElementById('closeBtn');
            
            if (isCompleted) {
                statusText.innerHTML = '<span class="completed">✓ 操作完成</span>';
                abortBtn.style.display = 'none';
                closeBtn.style.display = 'inline-block';
                clearInterval(timerInterval);
            } else if (isAborted) {
                statusText.innerHTML = '<span class="error">⚠ 操作已中止</span>';
                abortBtn.style.display = 'none';
                closeBtn.style.display = 'inline-block';
                clearInterval(timerInterval);
            } else if (isError) {
                statusText.innerHTML = '<span class="error">✗ 操作失败: ' + status + '</span>';
                abortBtn.style.display = 'none';
                closeBtn.style.display = 'inline-block';
                clearInterval(timerInterval);
            } else {
                statusText.innerHTML = '<span class="spinner"></span>' + status;
                abortBtn.style.display = 'inline-block';
                closeBtn.style.display = 'none';
            }
            
            // 更新详细信息
            document.getElementById('currentItem').textContent = currentItem || '-';
            document.getElementById('processedCount').textContent = processedCount;
            document.getElementById('totalCount').textContent = totalCount;
        }
        
        // 中止操作
        function abortOperation() {
            const abortBtn = document.getElementById('abortBtn');
            abortBtn.disabled = true;
            abortBtn.textContent = '正在中止...';
            
            if (typeof sketchup !== 'undefined') {
                sketchup.abortOperation();
            }
        }
        
        // 关闭对话框
        function closeDialog() {
            if (typeof sketchup !== 'undefined') {
                sketchup.closeDialog();
            }
        }
        
        // 页面加载完成后通知Ruby
        document.addEventListener('DOMContentLoaded', function() {
            startTimer();
            if (typeof sketchup !== 'undefined') {
                sketchup.dialogReady();
            }
        });
        
        // 全局函数，供Ruby调用
        window.updateProgress = updateProgress;
    </script>
</body>
</html>